/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.definitions;

/**
 *
 * @author abracadabuda
 */
public class RuleDefinition extends AbstractDefinition {
    private String name;
    private String catalog;
    private String schema;
    private String table;
    private String type;
    private String action;
    private String where;
    private String source;

    
    public RuleDefinition() {
        schema = AbstractDefinition.prefix;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {        
        this.source = source;
    }

    /**
     * returns SQL code for rule (CREATE OR REPLACE RULE...)
     * @return SQL code
     */
    public String getSQL() {
        String sql = "CREATE OR REPLACE RULE " + "\"" + name + "\" AS \n";
        sql += "ON " + type + " TO " + "\"" + schema + "\".\"" + table + "\"\n";
        sql += where.trim().equals("") ? "" : "WHERE " + where.trim() + "\n";
        sql += "DO " + action + "\n";
        sql += "(" + source + ");\n";
        
        return sql.replaceAll("%%%", "\"" + prefix + "\"");
    }
    
}